package com.leonzhangxf.ioc.importation;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

/**
 * The hello configuration selector.
 *
 * @author leonzhangxf
 */
public class HelloImportSelector implements ImportSelector {

    private static final String HELLO_IMPORT_TARGET_KEY = "hello";

    /**
     * Default would use {@link HelloWorldConfiguration}, and if the {@link EnableHello#hello()} is
     * {@link HelloEnum#HELLO_LEON}, the {@link HelloLeonConfiguration} would be used instead.
     */
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        if (importingClassMetadata.hasAnnotation(EnableHello.class.getName())) {
            Map<String, Object> annotationAttributes =
                importingClassMetadata.getAnnotationAttributes(EnableHello.class.getName());
            if (null != annotationAttributes) {
                Object o = annotationAttributes.get(HELLO_IMPORT_TARGET_KEY);
                if (o instanceof HelloEnum) {
                    HelloEnum helloEnum = (HelloEnum) o;
                    if (helloEnum == HelloEnum.HELLO_LEON) {
                        return new String[]{HelloLeonConfiguration.class.getName()};
                    }
                    return new String[]{HelloWorldConfiguration.class.getName()};
                }
            }
        }
        return new String[]{HelloWorldConfiguration.class.getName()};
    }
}
